package com.shijie.controller.common;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shijie.core.Result;
import com.shijie.core.ResultGenerator;
import com.shijie.domain.entity.Categories;
import com.shijie.domain.enums.EnumInterface;
import com.shijie.service.CategoriesService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/*
 *  通用的分类查询，用于erp和app
 *
 *  @author: smart boy
 *  @date: 2018-08-06
 */
@RestController
@RequestMapping("/categories")
public class ComCategoriesController {

    @Resource
	private CategoriesService categoriesService;

    /**
     * 分类节点树
     *
     *
     * @param type 分类类型 0：资讯分类 3：产品分类
     * @param status 0可用1不可用
     *
     * @return
     */
	@GetMapping
    public Result list(@RequestParam String type, @RequestParam(required = false) String status) {
        List<Categories> list = categoriesService.selectCategoriesByType(type, status);
        return ResultGenerator.success(list.get(0).getChildrens());
    }

    @GetMapping("/{id}")
    public Result detail(@PathVariable Integer id) {
        Categories categories = categoriesService.selectById(id);
        return ResultGenerator.success(categories);
	}

    /**
     * 父节点查询子节点信息
     * @param parentId
     * @param type 分类类型 0：资讯分类 3：产品分类
     * @return
     */
    @GetMapping("/{type}/{parentId}/@parentId")
    public Result list(@PathVariable Integer parentId, @PathVariable String type) {
        List<Categories> list = categoriesService.selectByParentId(parentId, type, EnumInterface.Status.VALID.ordinal());
        return ResultGenerator.success(list);
    }
}

